package com.sec.android.app.myfiles.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.format.Formatter;
import android.view.View;
import android.widget.TextView;

import com.sec.android.app.myfiles.R;

/**
 * Created by daesu83.kim on 2017-04-14.
 */

public class GmsDeleteResultDialog extends DialogFragment {

    private long mTotalSize;
    View icon;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Activity a = getActivity();
        AlertDialog.Builder builder = new AlertDialog.Builder(a);
        View view = View.inflate(a, R.layout.get_more_space_delete_result, null);
        TextView tv = (TextView) view.findViewById(R.id.total_size);
        tv.setText(Formatter.formatFileSize(a, mTotalSize));
        builder.setView(view);
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        icon = view.findViewById(R.id.icon);
        ((AnimationDrawable) icon.getBackground()).start();

        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    public void setTotalSize(long totalSize) {
        mTotalSize = totalSize;
    }

    public void showAllowingStateLoss(final FragmentManager fm, final String tag) {
        if (fm != null) {
            final FragmentTransaction t = fm.beginTransaction();
            t.add(this, tag);
            t.commitAllowingStateLoss();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        int size = getActivity().getResources().getDimensionPixelOffset(R.dimen.get_more_space_delete_result_icon_size);
        if (icon != null && icon.getLayoutParams() != null) {
            icon.getLayoutParams().height = size;
            icon.getLayoutParams().width = size;
        }
    }
}
